<head>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        body{
            font-size: 12px;
        }
        .result{
            font-weight: bold;
            color: red;
        }
        table{
            border-collapse: collapse;
            font-size: 12px;
        }
        tr{
            border-bottom: 1px solid black;
        }
        th{
            padding: 2px 15px 2px 15px;
        }
        td{
            text-align: center;
        }
        .bold{
            font-weight: bold;
        }
        #border{
            width: 100%;
        }
        #left{
            width: 25%;
            float: left;
            padding: 5px;
            font-size: 15px;
        }
        #right{
            width: 73%;
            float: left;
            padding: 5px;
        }
        .ul_left{
            margin-top: 20px;
        }
    </style>
</head>
<body>
<div id="border">
    <?php
    try{
        $db = new PDO("sqlsrv:Server=192.168.1.203\MSSQL2014;Database=ZXZH_WMS", "sa", "Zxzh88888888");
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $time_now = date("Y-m-d H:i:m");
        $from = $db->query("SELECT TOP 1 demo3, carno from log_out_pentu_check_auto_P1_new order by tkdat desc");
        $row = $from->fetch();
        $p1_no = $row['demo3'];
        $p1_car_no = $row['carno'];
        $from = $db->query("SELECT TOP 1 demo3, carno from log_out_pentu_check_auto_P2 order by tkdat desc");
        $row = $from->fetch();
        $p2_no = $row['demo3'];
        $p2_car_no = $row['carno'];
        $big_data['P1'] = $big_data['P2'] = array();
        $need_show = array();
        $need_data = array();
        foreach ($big_data as $p_num => $p_content){
            $need_data_num = 0;
            $need_show_num = 0;
            if(strcmp($p_num,'P1') == 0){
                $p_no = $p1_no;
            }
            else{
                $p_no = $p2_no;
            }
            if(strstr($p_no,'-')){
                $p_no = substr($p_no, strpos($p_no, "-"));
            }
            $p_no = intval($p_no);
            $p_no_scan = $p_no;
            $big_data[$p_no] = array();
            $str = "with a as
(SELECT MATNO, MNAME, DEMO7, DEMO11, PRDAT, DEMO8, QUANT, QUANT0, PONO, DEMO1, DEMO2, DEMO3, DEMO4, DEMO5, ORDNO,
case when CHARINDEX('-',DEMO8) > 0 then SUBSTRING(DEMO8,1,CHARINDEX('-',DEMO8)-1) else DEMO8 end DEMO8Int 
 from View_log_erp_out with(nolock) WHERE DEMO7 = '$p_num' AND DEMO11 = 'Y')
select * from a where DEMO8Int >= $p_no ORDER BY PRDAT ASC";
            $from = $db->query($str);
            if($from){
                $all_array = $from->fetchAll();
                $all_row_num = count($all_array);
            }
            $from = $db->query( $str);
            $row_num = 0;
            if(!$from){
                return;
            }
            while ( $row = $from->fetch() ) {
                $row_num++;
                if($p_no_scan != $row['DEMO8Int']){
                    foreach ($big_data[$row['DEMO7']] as $key => $value){
                        $need_data[$p_num][$row['DEMO8Int'] - 1][$need_data_num]['name'] = $key;
                        $need_data[$p_num][$row['DEMO8Int'] - 1][$need_data_num]['need'] = $big_data[$row['DEMO7']][$key]['NEED'];
                        $need_data[$p_num][$row['DEMO8Int'] - 1][$need_data_num]['now'] = $big_data[$row['DEMO7']][$key]['NOW'];
                        $need_data[$p_num][$row['DEMO8Int'] - 1][$need_data_num]['start_che'] = $big_data[$row['DEMO7']][$key]['start_che'];
                        $need_data_num++;
                    }
                    $big_data[$row['DEMO7']] = array();
                    $p_no_scan = $row['DEMO8Int'];
                }
                if(!array_key_exists($row['MNAME'], $big_data[$row['DEMO7']])){
                    $big_data[$row['DEMO7']][$row['MNAME']]['JG'] = 0;
                    $big_data[$row['DEMO7']][$row['MNAME']]['NEED'] = 0;
                    $big_data[$row['DEMO7']][$row['MNAME']]['NOW'] = 0;
                    $big_data[$row['DEMO7']][$row['MNAME']]['start_che'] = $row['DEMO1'];
                }

                if($row['QUANT'] > $row['QUANT0'] && $row['PRDAT'] > $time_now){
                    $show_str =  "<tr><td>".$row['MNAME']."</td><td>".$row['DEMO7']."</td><td>".$row['DEMO11']."</td><td>".$row['PRDAT']."</td><td>".$row['DEMO8Int']."</td><td>".$row['QUANT']."</td><td>".round($row['QUANT0'],0)."</td><td>".$row['PONO']."</td><td>".$row['DEMO1']."</td><td>".$row['DEMO2']."</td><td>".$row['DEMO3']."</td><td>".$row['DEMO4']."</td><td>".$row['DEMO5']."</td><td>".$row['ORDNO']."</td>";

                    $cha = $row['QUANT'] - $row['QUANT0'];
                    $matno = $row['MATNO'];
                    $need_ping = false;
                    $from_mstore = $db->query(  "SELECT matno, quant from View_log_mstore_unionMat WHERE cflag = 1 AND matno = '$matno'");

                    if(!$from_mstore){
                        $need_ping = true;
                        $need_num = $cha;
                    }
                    else{
                        $mstore_num = 0;
                        while ( $row_mstore = $from_mstore->fetch() ) {
                            $mstore_num += $row_mstore['quant'];
                        }
                        if($mstore_num < $cha){
                            $need_ping = true;
                            $need_num = $cha - $mstore_num;
                        }
                    }
                    if($need_ping){
                        $show_str = $show_str."<td class='result'>需要平库备</td>";
                        $db_kanban = new PDO("sqlsrv:Server=172.30.11.178;Database=StoneWMS_MES", "sa", "jk123");
                        $db_kanban->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                        $sn_array = array();
                        $num = 0;
                        $from_kanban = $db_kanban->query("select SN from T_BINMultTask where State = '已执行'and TaskCate like '%注塑%'");
                        while ($row = $from_kanban->fetch()){
                            $sn_array[$num] = $row['SN'];
                            $num++;
                        }
                        $from_kanban = $db_kanban->query("select Qty, SN from T_BINMultTask_d where Qty <>0 and QAD = '$matno'");
                        $yes_pingku = false;
                        $ping_num = 0;
                        if(!$from_kanban){
                            $yes_pingku = false;
                        }
                        else{
                            while ($row = $from_kanban->fetch()){
                                foreach ($sn_array as $value){
                                    if(strcmp($value,$row['SN']) == 0){
                                        $yes_pingku = true;
                                        $ping_num += $row['Qty'];
                                    }
                                }
                                reset($sn_array);
                            }
                        }
                        if(!$yes_pingku){
                            $show_str = $show_str."<td class='result'>平库也么</td>";
                        }
                        else{
                            if($need_num <= $ping_num){
                                $show_str = $show_str."<td class='result'>平库够，通知平库备</td>";
                            }
                            else{
                                $show_str = $show_str."<td class='result'>平库有但不够，通知计划</td>";
                            }
                        }

                    }
                    else{
                        //$show_str = $show_str."<td class='result'>立体库内够</td>";
                    }
                    $show_str = $show_str."</tr>";
                    $need_show[$p_no][$row['DEMO7']][$need_show_num] = $show_str;
                    $need_show_num++;
                }

                if(empty($big_data[$row['DEMO7']])){
                    echo "wow";
                    print_r($big_data[$row['DEMO7']]);
                }
                foreach ($big_data[$row['DEMO7']] as $key1 => $value1){
                    if(strcmp($key1, $row['MNAME']) == 0){
                        $big_data[$row['DEMO7']][$row['MNAME']]['NEED'] += $row['QUANT'];
                        $big_data[$row['DEMO7']][$row['MNAME']]['JG'] = 0;
                        $big_data[$row['DEMO7']][$row['MNAME']]['NOW'] += round($row['QUANT0'],0);
                    }
                    else{
                        $big_data[$row['DEMO7']][$key1]['JG']++;
                    }
                    if($big_data[$row['DEMO7']][$key1]['JG'] >= 3){
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['name'] = $key1;
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['need'] = $big_data[$row['DEMO7']][$key1]['NEED'];
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['now'] = $big_data[$row['DEMO7']][$key1]['NOW'];
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['start_che'] = $big_data[$row['DEMO7']][$key1]['start_che'];
                        unset($big_data[$row['DEMO7']][$key1]);
                        $need_data_num++;
                    };
                }
                //reset($big_data[$row['DEMO7']]);
                if($row_num == $all_row_num){
                    foreach ($big_data[$row['DEMO7']] as $key => $value){
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['name'] = $key;
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['need'] = $big_data[$row['DEMO7']][$key]['NEED'];
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['now'] = $big_data[$row['DEMO7']][$key]['NOW'];
                        $need_data[$p_num][$row['DEMO8Int']][$need_data_num]['start_che'] = $big_data[$row['DEMO7']][$key]['start_che'];
                        $need_data_num++;
                        unset($big_data[$row['DEMO7']][$key]);
                    }
                }
            }
        }
    }
    catch(PDOException $e){
        die ("Error!: " . $e->getMessage() . "<br/>");
    }
    ?>
    <div id="left">
        <p>下面是解盘功能，试试看。</p>
        <input id="palno" type="text" placeholder="输入托盘号" /><button onclick="delete_task()">一键解盘</button>
        <br>
        <br>
        <p>现在时间<?php echo date("Y-m-d H:i:m"); ?></p>
        <?php

        echo "<div class='bold'>现在涂装1线".$p1_no."圈".$p1_car_no."车</div>";

        echo "<div class='bold'>现在涂装2线".$p2_no."圈".$p2_car_no."车</div>";
        foreach ($need_data as $key => $value){
            echo "<ul class='ul_left'>涂装". $key;
            foreach ($value as $key1 => $value1){
                echo "<ul>第".$key1."圈";
                foreach ($value1 as $value2){
                    echo "<li>".$value2['name'].":".$value2['need']."|".$value2['now']."/".$value2['start_che'];
                    $need_pan_num = floor(($value2['now'] - $value2['need']) / 6);
                    if($need_pan_num > 0){
                        echo "额外多".$need_pan_num."盘";
                    }
                    echo "</li>";
                }
                echo "</ul>";
            }
            echo "</ul>";
        }
        ?>
        <br>

    </div>
    <div id="right">
        <table><tr><th>产品名称</th><th>涂装线</th><th>已排产</th><th>计划时间</th><th>圈数</th><th>出库总数量</th><th>已排产数量</th><th>小车数</th><th>小车号1</th><th>小车号2</th><th>小车号3</th><th>小车号4</th><th>小车号5</th><th>出库单号</th><th>结论</th><th>数据</th></tr>
            <?php
            //print_r("")
            foreach ($need_show as $value){
                foreach ($value as $value_1){
                    foreach ($value_1 as $show){
                        echo $show;
                    }
                }
            }
            ?>
        </table>
    </div>
</div>

<script>
    setInterval(function() {
        window.location.reload(true);
    },300000);

    function delete_task() {
        let palno = $('#palno').val();
        $.ajax({
            url:'delete_task.php?palno='+palno,
            type:'get',
            success: function (result) {
                alert(result);
                window.location.reload(true);
            }
        });
    }


</script>
</body>